package com.roadjava.GDOUGym.service.impl;

import com.roadjava.GDOUGym.bean.dto.PlaceDTO;
import com.roadjava.GDOUGym.bean.entity.GameDO;
import com.roadjava.GDOUGym.bean.entity.PlaceDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.convert.SiteConvert;
import com.roadjava.GDOUGym.mapper.SiteMapper;
import com.roadjava.GDOUGym.service.SiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class SiteServiceImpl implements SiteService {

    @Resource
    private SiteMapper siteMapper;

    @Resource
    private SiteConvert siteConvert;

    @Override
    public ResultDTO<List<PlaceDTO>> findSite(PlaceDTO dto) {
        List<PlaceDO> dos = siteMapper.findSite(dto);
        List<PlaceDTO> dtos = siteConvert.dos2dtos(dos);
        long total = siteMapper.selectCount(dto);
        return ResultDTO.buildSuccess(dtos,total);
    }

    @Override
    public ResultDTO<String> add(PlaceDTO placeDTO) {
        PlaceDTO dto = new PlaceDTO();
        String no;
        List<PlaceDO> placeDOList = siteMapper.findSite(dto);
        if(placeDOList.size() == 0){
            no = "0001";
        }else{
            String endNo = placeDOList.get(placeDOList.size()-1).getPlaceNo();
            long total = Integer.parseInt(endNo)+1;
            // 把dto转为do对象
            no= "";
            if(total>=0 && total<10){
                no = "000"+total;
            }else if(total>=10 && total<100){
                no = "00"+total;
            }else if(total>=100 && total<1000){
                no = "0"+total;
            }else{
                no = total+"";
            }
        }

        // 把dto转为do对象
        PlaceDO placeDO = siteConvert.dto2do(placeDTO);
        placeDO.setPlaceNo(no);
        int result = siteMapper.add(placeDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("添加成功");
        }else {
            return ResultDTO.buildSuccess("添加失败");
        }
    }

    @Override
    public ResultDTO<String> change(PlaceDTO placeDTO) {
        // 把dto转为do对象
        PlaceDO placeDO = siteConvert.dto2do(placeDTO);
        int result = siteMapper.change(placeDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<String> delete(PlaceDTO placeDTO) {
        // 把dto转为do对象
        PlaceDO placeDO = siteConvert.dto2do(placeDTO);
        int result = siteMapper.delete(placeDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("删除成功");
        }else {
            return ResultDTO.buildSuccess("删除失败");
        }
    }

}
